package demo.DataStrucAndAlgo.Easy_pritice;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Stack;

/**
 * @Auther: hjy(lc))
 * @Date: 2022/8/26-08-26-8:59
 * @Description：demo.DataStrucAndAlgo.Easy_pritice
 */
public class ThreeNum_Cal {
    public static void main(String[] args) {
//        int[] nums = {6,-5,-6,-1,-2,8,-1,4,-10,-8,-10,-2,-4,-1,-8,-2,8,9,-5,-2,-8,-9,-3,-5};
//        System.out.println(Math.abs(-3));
//        System.out.println("---" + threeSum2(nums));
        int[] nums = {-2,-1,-1,1,1,2,2};
        //System.out.println(fourSum(nums,0));
        Stack<Character> stack = new Stack();
        stack.add('(');
        System.out.println(stack.pop());
        System.out.println(stack.size()==0);

    }
    public static List<List<Integer>> fourSum(int[] nums, int target) {

        List<List<Integer>> result = new ArrayList();
        if(nums.length < 4){
            return result;
        }
        List<Integer> list;
        Arrays.sort(nums);
        for(int i = 0; i <= nums.length-4; i++){
            if(i > 0 && nums[i] == nums[i-1]){
                continue;
            }
            for(int j = nums.length-1;j >= i + 3; j--){
                if(j < nums.length-1 && nums[j] == nums[j+1]){
                    continue;
                }
                int L = i + 1;
                int R = j - 1;
                while(L < R){
                    int sum = nums[i] + nums[L] + nums[R] + nums[j];
                    if(sum == target){
                        list = new ArrayList();
                        list.addAll(Arrays.asList(nums[i],nums[L],nums[R],nums[j]));
                        result.add(list);
                        L++;
                        R--;
                        while(nums[L]==nums[L-1]){
                            L++;
                        }
                        while(nums[R] == nums[R+1]){
                            R--;
                        }
                    }else if(sum <= target){
                        while(L<R-1 && nums[L] == nums[L+1]){
                            L++;
                        }
                        L++;
                    }else{
                        while(L<R-1 && nums[R] == nums[R-1]){
                            R--;
                        }
                        R--;
                    }


                }
            }
        }
        return result;
    }
    public static List<List<Integer>> threeSum(int[] nums) {
        if(nums.length < 3){
            return null;
        }
        List<List<Integer>> result = new ArrayList<>();
        List<Integer> temp;
        for(int i = 0; i < nums.length-3; i++){
            for(int j = i +1; j < nums.length-2; j++){
                for(int k = j + 1; k < nums.length; k++){
                    if(nums[i] + nums[j] + nums[k] == 0){
                        temp = new ArrayList();
                        temp.addAll(Arrays.asList(nums[i],nums[j],nums[k]));
                        result.add(temp);
                        System.out.println(temp);
                        System.out.println(result);
                        //temp.clear();
                    }
                }
            }
        }
        return result;
    }
    public static List<List<Integer>> threeSum2(int[] nums) {
        if(nums.length < 3){
            return null;
        }
        List<List<Integer>> result = new ArrayList();
        List<Integer> temp;
        Arrays.sort(nums);
        int L = 0, R = nums.length-1;
        int i = 0;
        for (i = 0; i < nums.length; i++) {
            if(nums[i] > 0){
                break;
            }
            if(i > 0 && nums[i] == nums[i-1]){
                continue;
            }
            L = i + 1;
            R = nums.length-1;

            while(L<R){
                if(nums[L] + nums[i] + nums[R] == 0){
                    temp = new ArrayList();
                    temp.addAll(Arrays.asList(nums[i],nums[L],nums[R]));
                    System.out.println(temp);
                    result.add(temp);
                    System.out.println(result);
                    while(L < R && nums[L] == nums[L+1]){
                        L++;
                    }
                    while(L < R && nums[R] == nums[R-1]){
                        R--;
                    }
                    L++;
                    R--;
                }else if(nums[L] + nums[i] + nums[R] < 0){
                    L++;
                }else{
                    R--;
                }

            }
        }
        return result;
    }
}
